<?php

namespace App\Service;

use App\Constants\ErrorCode;
use App\Model\AuthUser;
use App\Model\ConsultantApply;
use App\Model\LoginLog;
use App\Model\User;
use App\Model\UserDatum;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;

class UserService extends BaseService
{

    /**
     * @Inject()
     * @var User
     */
    public $userModel;


    /**
     * 用户登录
     * @param $uid
     * @param int $platform
     * @param string $accountType
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function userLogin($uid, $platform = 1, $accountType = 'wechat')
    {

        //查询登录信息
        $user = User::jwtLoginInfo($uid);

        if (!is_null($user)) {
            try {

                $token = auth('jwt')->login($user);
                // 加入登录名单
                redis()->set('user:userToken:' . $uid, $token);


                //记录登录信息
                LoginLog::addLoginLog($uid, $accountType, $platform);


                return $this->response->returnSuccess([
                    'token' => $token,
                    'userInfo' => UserDatum::userInfo($uid)
                ]);
            } catch (\Exception $exception) {
                recordExceptionLog('登录JWT失败', $exception->getMessage());
                return $this->response->returnFail();
            }
        } else {
            return $this->response->returnFail();
        }

    }


    /**
     * 咨询师端用户登录
     * @param $uid
     * @param $platform
     * @param $accountType
     * @return ResponseInterface
     */
    public function consultantUserLogin($uid, $platform = 1, $accountType = 'wechat')
    {

        //查询登录信息
        $user = User::jwtLoginInfo($uid);

        if (!is_null($user)) {
            try {

                $token = auth('jwt')->login($user);
                // 加入登录名单
                redis()->set('consultantUser:userToken:' . $uid, $token);


                //记录登录信息
                LoginLog::addLoginLog($uid, $accountType, $platform);


                //处理
                $authentication = 0;
                if ($user['role'] >= 1) {
                    $authentication = 1;
                }else{
                    // 查看是否有正在初次审核的状态
                    $apply = ConsultantApply::where(['uid'=>$uid,'type'=>1,'status'=>0])->exists();
                    if ($apply){
                        $authentication = 2;//正在审核中
                    }

                }

                // todo 咨询师信息 后续再加
                return $this->response->returnSuccess([
                    'token' => $token,
                    'authentication' => $authentication,
                    'consultantInfo'=>UserDatum::consultantUserInfo($uid)
                ]);
            } catch (\Exception $exception) {
                recordExceptionLog('登录JWT失败', $exception->getMessage());
                return $this->response->returnFail();
            }
        } else {
            return $this->response->returnFail();
        }

    }


    public function adminUserLogin($id)
    {

        $adminUser = AuthUser::where(['id' => $id, 'status' => 1])->first();

        if (!is_null($adminUser)) {
            try {

                $token = auth('jwt_admin')->login($adminUser);
                // 加入登录名单
                redis()->set('admin-user:userToken:' . $id, $token);

                //记录登录信息
//                LoginLog::addLoginLog($uid, $accountType, $platform);


                return $this->response->returnSuccess([
                    'token' => $token
                ]);
            } catch (\Exception $exception) {
                recordExceptionLog('后台登录JWT失败', $exception->getMessage());
                return $this->response->returnFail();
            }
        }
    }


}
